package com.kgcx.schedule.service;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttPersistenceException;
import org.eclipse.paho.client.mqttv3.MqttTopic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.kgcx.common.client.organization.pojo.Driver;
import com.kgcx.common.constant.NotifyTypeEnum;
import com.kgcx.common.mqtt.TopicConst;
import com.kgcx.common.utils.AuroraPush;
import com.kgcx.schedule.domain.ScheduleRecordDomain;

@Component
public class ScheduleNotifyService {
    private static final Logger LOG = LoggerFactory.getLogger(ScheduleNotifyService.class);

    @Value("${kgcx.schedule.notify-driver-order-type:jpush}")
    protected String notifyDriverOrderType;
    @Autowired(required = false)
    private MqttClient mqttProducer;

    public void notifyOldDriver(ScheduleRecordDomain record, Long driver_id) {
        try {
            // 发送通知
            AuroraPush.pushTargetForDriver(String.valueOf(driver_id),
                    String.format("您的订单，订单编号【%s】已成功改派。", record.getOrder_no()));

            // 发送消息
            AuroraPush.pushTargetMsgForDriver(TopicConst.CHANGE_DRIVER_SUCCESS, record.getOrder_no(),
                    String.valueOf(record.getDriver_id()), AuroraPush.PLATFORM_ANDROID);

            LOG.info(String.format("订单[%s]，给司机[%d]推送改派成功消息成功，推送方式为jpush。", record.getOrder_no(), driver_id));
        } catch (Exception e) {
            LOG.error(String.format("订单[%s]，给司机[%d]推送改派成功消息失败，推送方式为jpush。", record.getOrder_no(), driver_id), e);
        }
    }

    public void notifyDriver(ScheduleRecordDomain record, Driver driver) {
        try {
            if (NotifyTypeEnum.MQTT.equals(notifyDriverOrderType)) {
                notifyDriverByMqtt(record, driver);
            } else if (NotifyTypeEnum.JPUSH.equals(notifyDriverOrderType)) {
                notifyDriverByMqttJpush(record, driver);
            } else {
                notifyDriverByMqttJpush(record, driver);
            }

            LOG.info(String.format("订单[%s]，给司机[%d]推送订单信息成功，推送方式为%s。", record.getOrder_no(), record.getDriver_id(),
                    notifyDriverOrderType));
        } catch (Exception e) {
            LOG.error(String.format("订单[%s]，给司机[%d]推送订单信息失败，推送方式为%s。", record.getOrder_no(), record.getDriver_id(),
                    notifyDriverOrderType), e);
        }
    }

    private void notifyDriverByMqtt(ScheduleRecordDomain record, Driver driver)
            throws MqttPersistenceException, MqttException {
        if (mqttProducer == null) {
            LOG.warn(String.format("订单[%s]，MQTT功能未开启，转为jpush推送。请确认kgcx.mqtt.enable配置属性的值是否为true。",
                    record.getOrder_no()));
            notifyDriverByMqttJpush(record, driver);
            return;
        }

        MqttMessage message = new MqttMessage();
        message.setQos(1);
        message.setRetained(false); // 是否是实时发送的消息
        message.setPayload(record.getOrder_no().getBytes());

        MqttTopic topic = mqttProducer.getTopic(TopicConst.SCHEDULE_ORDER_SUCCESSFUL + "." + record.getDriver_id());
        topic.publish(message);
    }

    private void notifyDriverByMqttJpush(ScheduleRecordDomain record, Driver driver) {
        // 发送通知
        AuroraPush.pushTargetForDriver(String.valueOf(record.getDriver_id()), "您接到一笔新的派车订单，请及时为乘客进行服务。");

        // 发送消息
        AuroraPush.pushTargetMsgForDriver(TopicConst.SCHEDULE_ORDER_SUCCESSFUL, record.getOrder_no(),
                String.valueOf(record.getDriver_id()), AuroraPush.PLATFORM_ANDROID);
    }
}
